
<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: register.php
| Author: Nick Jones (Digitanium)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once "maincore.php";
require_once THEMES."templates/header.php";
include LOCALE.LOCALESET."register.php";
include LOCALE.LOCALESET."user_fields.php";

$avloserid=$_GET['avloserid'];

$db = mysql_connect($GLOBALS['db_host'], $GLOBALS['db_user'], $GLOBALS['db_password']);
				mysql_select_db($GLOBALS['db_name'], $db);  

$query2="SELECT * 
		 FROM avloser 
		 WHERE avloserid='$avloserid'";
	$result2=mysql_query($query2);
	$num2=mysql_numrows($result2);
	
	$ii=0;
	while ($ii < $num2) {
		
		$fornavnet=mysql_result($result2,$ii,"fornavn");
		$etternavnet=mysql_result($result2,$ii,"etternavn");
	++$ii;
	}
	
	
	$query3="SELECT klasse 
			 FROM oppkjoring";
	$result3=mysql_query($query3);
	
	$options="";
	
	while ($row=mysql_fetch_array($result3)) {
	
		$klasse=$row["klasse"];
		$options.="<OPTION VALUE=\"$klasse\">".$klasse;
	}
	
if(isset($_GET['opprett']))
{
	
	$opprett=$_GET['opprett'];
	if($opprett == 'ja')
	{
		
		if(isset($_POST['utgaar']))
		{
			$utgaar=$_POST['utgaar'];
			$avloserid=$_GET['avloserid'];
		

			
			
			
			$query1="INSERT INTO forerkort VALUES ('',$avloserid,$utgaar)";
			mysql_query($query1);
			$ret =  mysql_insert_id();
			echo "<meta http-equiv='refresh' content='0;url=forerkort.php?forerkortid=$ret&avloserid=$avloserid'>";
			
		
		}else
		{
			echo" <h1 align='center'>Du må fylle inn &aring;ret f&oslash;rerkortet er gyldig til!</h1>";	
		}
		
	}

}



else if(isset($_GET['forerkortid']))
{
	$fkid=$_GET['forerkortid'];
	
	
	$db = mysql_connect($GLOBALS['db_host'], $GLOBALS['db_user'], $GLOBALS['db_password']);
	mysql_select_db($GLOBALS['db_name'], $db); 
	
	$query="SELECT * 
			FROM forerkort 
			WHERE forerkortid='$fkid'";
	$result=mysql_query($query) or die(mysql_error());
	$num1=mysql_numrows($result);
	
	
	$i=0;
	while ($i < $num1) {
		$avloserid=mysql_result($result,$i,"avloserid");
		$utgar=mysql_result($result,$i,"utgaar");
	++$i;
	}
		
	echo "

	<br></br>
	<table class='capmain' border='0' cellpadding='0' align='center'>
		<tr>
			<th><font style='text-transform: capitalize;'>Oppkj&oslash;rings info for $fornavnet  $etternavnet </font></th>
		</tr>
	</table>

	
	
	
	

	<table class='imagetable' cellpadding='0' cellspacing='0' align='center'>
			
			
			<tr>	<th width='25%'>Fornavn:</th>	<th >Etternavn:</th>	<th>Utg&aring;r:	</th>	</tr>
					<tr>	<td width='25%' class='a'>	$fornavnet	</td>	<td class='a' width='25%'>$etternavnet</td>	<td class='a' >$utgar</td>	</tr>
			<tr>	<th>Klasse p&aring; F&oslash;rerkort:</th><td colspan='2'>
			<form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&leggtil=1&utgaar=$utgar' method='post'>
			<SELECT NAME='klasse_pa_forerkort[]'><OPTION VALUE=0>$options</SELECT>
			<input type='Submit' value='Fortsett'></form>
			
			
			
			</td></tr>
			<tr><th>Oppkj&oslash;rte klasser:</th>
			<td colspan='2'></td>

			</tr>
			<tr>	<th colspan='3'>&nbsp;</th></tr>
			</table><br></br>	
	
		";

}else if(isset($_GET['leggtil']))
{	$leggtil=$_GET['leggtil'];
	if($leggtil == 1){
		
		$fkid=$_GET['fkid'];
		$avloserid=$_GET['avloserid'];
		$leggtil=$_GET['leggtil'];
		 
		$klasse_pa_FK=implode(',',$_POST['klasse_pa_forerkort']);
		$utgar=$_GET['utgaar'];
		
		$query1="INSERT INTO forerkortklasse VALUES ('','$klasse_pa_FK','$fkid')";
		mysql_query($query1);
		$nylig_lagt_inn =  mysql_insert_id();
		
		
		$query="SELECT * 
				FROM forerkortklasse 
				WHERE forerkortid='$fkid'";
		$result2=mysql_query($query) or die(mysql_error());
		$num1=mysql_numrows($result2);
		
		echo "

			<br></br>
			<table class='capmain' border='0' cellpadding='0' align='center'>
				<tr>
					<th><font style='text-transform: capitalize;'>Oppkj&oslash;rings info for $fornavnet  $etternavnet </font></th>
				</tr>
			</table>
		
			
					
			<table class='imagetable' cellpadding='0' cellspacing='0' align='center'>
			
					
					<tr>	<th width='25%'>Fornavn:</th>	<th >Etternavn:</th>	<th>Utg&aring;r:	</th>	</tr>
					<tr>	<td width='25%'class='a'>	$fornavnet	</td>	<td class='a' width='25%'>$etternavnet</td>	<td class='a' >$utgar</td>	</tr>		
					<tr>		
							<th width='25%'>Klasse p&aring; F&oslash;rerkort:	</th>
							<td width='25%'> 
								<form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&leggtil=1&utgaar=$utgar' method='post'>
								<SELECT NAME='klasse_pa_forerkort[]'><OPTION VALUE=0>$options</SELECT>	
								<input type='Submit' value='Legg til'>		
								</form>
							</td>	
							<td>";
								
								$i=0;
								while ($i < $num1) {
								$klasse=mysql_result($result2,$i,"klasse");
								echo"$klasse &nbsp;";
								++$i;
								}
								echo "
							
							</td>
													</tr>
					<tr>	<th width='25%'>Oppkj&oslash;rte klasser:</th><td colspan='2' width='25%'></td>		</tr>
										
					
					<tr>	<th colspan='3'><form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&leggtil=2&utgaar=$utgar' method='post'><input type='Submit' value='Neste'></form></th></tr>
					</table><br></br>";
	
	}else if($leggtil == 2){

		
		$fkid=$_GET['fkid'];
		$avloserid=$_GET['avloserid'];
		$leggtil=$_GET['leggtil'];
		if(isset($_POST['oppkjoring_klasse'])){
			$oppkjoring_klasse=implode(',',$_POST['oppkjoring_klasse']); }
		
		$utgar=$_GET['utgaar'];
		
		
		$query="SELECT * 
				FROM forerkortklasse 
				WHERE forerkortid='$fkid'";
		$result2=mysql_query($query) or die(mysql_error());
		$num1=mysql_numrows($result2);
		
		if(isset($_POST['oppkjoring_klasse'])){$query1="INSERT INTO oppkjoringklasse VALUES ('','$oppkjoring_klasse','$fkid')";
		mysql_query($query1);
		$nylig_lagt_inn2 =  mysql_insert_id();}
		
		$query="SELECT * 
				FROM oppkjoringklasse 
				WHERE forerkortid='$fkid'";
		$result3=mysql_query($query) or die(mysql_error());
		$num2=mysql_numrows($result3);
	
	
		
		
		echo "

			<br></br>
			<table class='capmain' border='0' cellpadding='0' align='center'>
				<tr>
					<th><font style='text-transform: capitalize;'>Oppkj&oslash;rings info for $fornavnet  $etternavnet </font></th>
				</tr>
			</table>
		
			
			<table class='imagetable' cellpadding='0' cellspacing='0' align='center'>
					
					<tr>	<th width='25%'>Fornavn:</th>	<th >Etternavn:</th>	<th>Utg&aring;r:	</th>	</tr>
					<tr>	<td width='25%' class='a'>	$fornavnet	</td>	<td class='a' width='25%'>$etternavnet</td>	<td class='a' >$utgar</td>	</tr>		
					<tr>		<th width='25%'>Klasse p&aring; F&oslash;rerkort:	</th>
								<td  colspan='2'>
						";
						
						$i=0;
						while ($i < $num1) {
						$klasse=mysql_result($result2,$i,"klasse");
						echo"$klasse &nbsp;";
						++$i;
						}
	
						
						echo "

						</td></tr>
						<tr><th width='25%'>Oppkj&oslash;rte klasser:</th>
						<td width='25%'>
						<form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&leggtil=2&utgaar=$utgar' method='post'>
						<SELECT NAME='oppkjoring_klasse[]'><OPTION VALUE=0>$options</SELECT>
						<input type='Submit' value='Legg til'></form>
						
						</td>
						<td>
						";
						
						$i=0;
						while ($i < $num2) {
						$klasse=mysql_result($result3,$i,"klasse");
						echo"$klasse &nbsp;";
						++$i;
						}
						
						echo" </td>					
					</tr>
					<tr>	<th colspan='3'><form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&ferdig=ja&utgaar=$utgar' method='post'><input type='Submit' value='Ferdig'></form></th></tr>
					</table><br></br>";
	}


}else if(isset($_GET['ferdig'])){
		$fkid=$_GET['fkid'];
		$utgar=$_GET['utgaar'];
		$query="SELECT * 
				FROM forerkortklasse 
				WHERE forerkortid='$fkid'";
		$result2=mysql_query($query) or die(mysql_error());
		$num1=mysql_numrows($result2);
		
		
		$query="SELECT * 
				FROM oppkjoringklasse 
				WHERE forerkortid='$fkid'";
		$result3=mysql_query($query) or die(mysql_error());
		$num2=mysql_numrows($result3);
	
	
		echo "

		<br></br>
				<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				
				<a href='profil.php?avloserid=$avloserid'><b><---  Tilbake til profil</b></a></p>
			
			<table class='capmain' border='0' cellpadding='0' align='center'>
				<tr>
					<th><font style='text-transform: capitalize;'>Oppkj&oslash;rings info for $fornavnet  $etternavnet </font></th>
				</tr>
			</table>
		
			
			<table class='imagetable' cellpadding='0' cellspacing='0' align='center'>
					
					<tr>	<th width='25%'>Fornavn:</th>	<th >Etternavn:</th>	<th>Utg&aring;r:	</th>	</tr>
					<tr>	<td width='25%' class='a'>	$fornavnet	</td>	<td class='a' width='25%'>$etternavnet</td>	<td class='a' >$utgar</td>	</tr>		
					<tr>		<th width='25%'>Klasse p&aring; F&oslash;rerkort:	</th>
								<td  colspan='2'>
						";
						
						$i=0;
						while ($i < $num1) {
						$klasse=mysql_result($result2,$i,"klasse");
						echo"$klasse &nbsp;";
						++$i;
						}
	
						
						echo "
						
						
			
			
			
						</td></tr>
						<tr><th width='25%'>Oppkj&oslash;rte klasser:</th>
						
						<td colspan='2'>
						";
						
						$i=0;
						while ($i < $num2) {
						$klasse=mysql_result($result3,$i,"klasse");
						echo"$klasse &nbsp;";
						++$i;
						}
						
						echo" </td>					
					</tr>
					
					<tr>	
					<th><form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&slett=ja' method='post'><input type='Submit' value='Slett'></form></th>
					<th colspan='2'><form action='forerkort.php?avloserid=".$avloserid."&fkid=".$fkid."&leggtil=1&utgaar=$utgar'  method='post'>
					
					
					<input type='Submit' value='Endre'></form>
					
					</th></tr>
					
					</table><br></br>";
					



}else if(isset($_GET['slett'])){
	//Henter avloser id fra URL for å hente riktig kontrakter fra DB
$avloserid=$_GET['avloserid'];
$forerkortid=$_GET['fkid'];

//Oppkobling mot databasen. Opkoblingsinfo ligger i config.php
	

$query6="DELETE 
		 FROM oppkjoringklasse 
		 WHERE oppkjoringklasse.forerkortid=$forerkortid"; 
		mysql_query($query6);
		
$query7="DELETE 
		 FROM forerkortklasse 
		 WHERE forerkortklasse.forerkortid=$forerkortid";
		mysql_query($query7);
				
$query8="DELETE 
		 FROM forerkort  
		 WHERE forerkort.avloserid=$avloserid"; 
		mysql_query($query8); 
				

echo "<meta http-equiv='refresh' content='0;url=profil.php?avloserid=$avloserid'>";

}else{
	
	
	
	$avloserid=$_GET['avloserid'];
	$db = mysql_connect($GLOBALS['db_host'], $GLOBALS['db_user'], $GLOBALS['db_password']);
	mysql_select_db($GLOBALS['db_name'], $db); 
	
	$query="SELECT * 
			FROM avloser 
			WHERE avloserid='$avloserid'";
	$result=mysql_query($query);
	$num1=mysql_numrows($result);
	
	$i=0;
	while ($i < $num1) {
		$fornavn=mysql_result($result,$i,"fornavn");
		$etternavn=mysql_result($result,$i,"etternavn");
	++$i;
	}
	
	$query="SELECT avloserid 
			FROM avloser 
			WHERE avloserid='$avloserid'";
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	
	$i=0;
	while ($i < $num) {
	$avloserid=mysql_result($result,$i,"avloserid");
	++$i;
	}
	
	echo "
	
		<br></br>
		<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='profil.php?avloserid=$avloserid'><b><---  Tilbake til profil</b></a></p>
		<table class='capmain' border='0' cellpadding='0' align='center' >
			<tr>
				<th><font style='text-transform: capitalize;'>Oppkj&oslash;rings info for $fornavn  $etternavn </font></th>
			</tr>
		</table>
	
		<form action='forerkort.php?avloserid=".$avloserid."&opprett=ja' method='post'>	
		<table class='imagetable' cellpadding='0' cellspacing='0' align='center'>
			
			
			<tr>	<th width='25%'>Fornavn:</th>	<th >Etternavn:</th>	<th>Utg&aring;r:	</th>	</tr>
					<tr>	<td width='25%' class='a'>	$fornavnet	</td>	<td class='a' width='25%'>$etternavnet</td>	<td class='a'><input type='text' name='utgaar' value='' </td>	</tr>	
					<tr>	<th colspan='3'><input type='Submit' value='Neste'></th></tr></form>
				
	</table><br></br>";
}

require_once THEMES."templates/footer.php";
?>